package gui;

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import database.DBconn;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class ChatMenu extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	
	ImageIcon picture;
	private JTable table;

	public ChatMenu(JPanel loginpanel, final String id) throws SQLException {
		loginpanel.removeAll();
		loginpanel.setLayout(null);
		
		String sql="SELECT * FROM userinfo WHERE u_id = "+id+";";			
		DBconn.getConnection();
        Statement stmt = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rset = stmt.executeQuery(sql);
        rset.next();
        
        byte[] imagedata=rset.getBytes("u_picture");
        picture=new ImageIcon(imagedata);
        
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 318, 587);
		loginpanel.add(panel);
		
		JButton btnNewButton = new JButton("back");
		btnNewButton.setBounds(10, 553, 89, 23);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new MaskLoginMenu(panel).setVisible(true);
				
			}
		});
		panel.setLayout(null);
		panel.add(btnNewButton);
		
		JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBounds(10, 11, 106, 139);
		panel.add(desktopPane);
		desktopPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(picture);
		lblNewLabel.setBounds(0, 0, 106, 139);
		desktopPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(162, 11, 146, 20);
		textField.setText(rset.getString("u_name"));
		textField.setEditable(false);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(126, 11, 46, 14);
		panel.add(lblName);
		
		JLabel lblNewLabel_1 = new JLabel("DOB");
		lblNewLabel_1.setBounds(126, 42, 46, 14);
		panel.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(162, 39, 146, 20);
		textField_1.setText(rset.getString("u_dob"));
		textField_1.setEditable(false);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Email");
		lblNewLabel_2.setBounds(126, 67, 46, 14);
		panel.add(lblNewLabel_2);
		
		textField_2 = new JTextField();
		textField_2.setBounds(162, 64, 146, 20);
		textField_2.setText(rset.getString("u_email"));
		textField_2.setEditable(false);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("You Sign In As :");
		lblNewLabel_3.setBounds(126, 107, 114, 14);
		panel.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(126, 130, 135, 20);
		textField_3.setText(rset.getString("u_username"));
		textField_3.setEditable(false);
		panel.add(textField_3);
		textField_3.setColumns(10);
		
		JButton btnSearch = new JButton("Search New Friends");
		btnSearch.setBounds(10, 161, 298, 23);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				SearchFriend.main(null, id);
				
			}
		});
		panel.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 208, 298, 338);
		panel.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"No.", "Name", "Chat"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(39);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(233);
		
		AbstractAction chat = new AbstractAction()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	new ChatPrivate().setVisible(true);
		    }
		    
		};

		ButtonColumn buttonColumn = new ButtonColumn(table, chat, 2);
		buttonColumn.setMnemonic(KeyEvent.VK_D);
		
		
		scrollPane.setViewportView(table);
		showFriendList(id);				
		loginpanel.validate();
		loginpanel.repaint();	

	}
	
	private void showFriendList(String uid){
		DefaultTableModel model=(DefaultTableModel) table.getModel();
	    String sql="SELECT f_accept,u_name FROM userinfo, friendlist WHERE friendlist.f_verifyrequest = '1' AND friendlist.f_verifyaccept = '1'  AND userinfo.u_id = "+uid+"; ";
	    try {
	    	//Connection con;
	    	DBconn.getConnection();
		    Statement stmt = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
	        ResultSet rs = stmt.executeQuery(sql);
	      	while(rs.next())
	      	{
	      	  String id = rs.getString("f_accept");
	      	  
	      	String sql1="SELECT u_name FROM userinfo WHERE u_id = "+id+"; ";
	      	 Statement stmt1 = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
	      	ResultSet rs1 = stmt1.executeQuery(sql1);
	      	rs1.next();
	      		
	      	  String name = rs1.getString("u_name");	
	          model.addRow(new Object []{id, name});  
	      	}
	         
	    }
	    catch(Exception e1) {
	        JOptionPane.showMessageDialog(null, e1.getMessage());
	    }
	}
	
	
}
